//
//  ICMGetCarSearchView.m
//  ICrEAM
//
//  Created by kuroro on 16/11/1.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMGetCarSearchView.h"
#import "ICMGetCarSearchTableViewCell.h"

@interface ICMGetCarSearchView()
@property (strong,nonatomic) UITableView *getCarSearchView;
@property (strong,nonatomic) NSMutableArray *getCarSearchModels;
@property (nonatomic, assign) BOOL hasNext;
@property (nonatomic, assign) int pageNo;
@end

@implementation ICMGetCarSearchView

-(NSMutableArray *)getCarSearchModels{
    if (!_getCarSearchModels) {
        _getCarSearchModels = [NSMutableArray array];
    }
    return _getCarSearchModels;
}

- (instancetype)initICMGetCarSearchViewWithFrame:(CGRect)frame dataArray:(NSArray *)dataArray{
    if (self = [super initWithFrame:frame]) {
        __weak typeof (self) weakSelf = self;
        self.getCarSearchView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        self.getCarSearchView.delegate = self;
        self.getCarSearchView.dataSource = self;
        self.getCarSearchView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [self.getCarSearchView registerClass:[ICMGetCarSearchTableViewCell class] forCellReuseIdentifier:@"ICMGetCarSearchTableViewCell"];
        [self addSubview:self.getCarSearchView];
        [self.getCarSearchView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.mas_top);
            make.bottom.equalTo(weakSelf.mas_bottom);
            make.left.equalTo(weakSelf.mas_left);
            make.right.equalTo(weakSelf.mas_right);
        }];
        
    }
    return self;
}

+ (instancetype)creatICMGetCarSearchViewWithFrame:(CGRect)frame dataArray:(NSArray *)dataArray{
    return [[ICMGetCarSearchView alloc] initICMGetCarSearchViewWithFrame:frame dataArray:dataArray];
}

- (void)setModels:(NSArray *)models{
    [self.getCarSearchModels removeAllObjects];
    //上拉加载
    if (models.count > 0) {
        self.pageNo = 1;
        self.hasNext = YES;
        [self setRefresh];
    } else {
        self.getCarSearchView.mj_footer.hidden = YES;
        [self showString:@"暂无相关搜索结果" forSeconds:1.5];
    }
    [self.getCarSearchModels addObjectsFromArray:models];
    [self.getCarSearchView reloadData];
}

//上拉加载
- (void)setRefresh {
    __weak ICMGetCarSearchView *my = self;
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (self.hasNext) {
            self.pageNo++;
            [my loadMoreData];
        } else {
            [self.getCarSearchView.mj_footer endRefreshingWithNoMoreData];
        }
    }];
    
    [footer setTitle:@"" forState:MJRefreshStateIdle];
    [footer setTitle:@"正在加载" forState:MJRefreshStateRefreshing];
    [footer setTitle:@"没有更多数据了" forState:MJRefreshStateNoMoreData];
    self.getCarSearchView.mj_footer = footer;
}

- (void)loadMoreData
{
    if([self.delegate respondsToSelector:@selector(loadMoreDataWithPage:WithText:)])
    {
        [self.delegate loadMoreDataWithPage:self.pageNo WithText:self.condition];
    }
}

//加载更多数据
- (void)loadMoreDataWithData:(id)data
{
    if ([data[@"CODE"] integerValue] == 200) {
        if (IsArr(data[@"TT_RECEPTION_VEHICLE_ORDER"])) {
            NSArray *dataArray = data[@"TT_RECEPTION_VEHICLE_ORDER"];
            if (dataArray.count > 0) {
                for (NSDictionary *searchData in dataArray) {
                    ICMGetCarSearchModel *model = [[ICMGetCarSearchModel alloc]init];
                    model.getCarId = searchData[@"RECEPTION_NO"];
                    model.licenceNumber = searchData[@"LICENSE"];
                    model.time = [searchData[@"CREATE_DATE"] stringValue];
                    model.vehicleType = searchData[@"MODEL_CODE"];
                    model.vehicleTypeName = searchData[@"MODEL_NAME"];
                    ParameterModel *Pmodel = [ICrEAMTool getCodeModelForCode:[NSString stringWithFormat:@"%@",searchData[@"RECEPTION_STATUS"]]];
                    model.RECEPTION_STATUS = [NSString stringWithFormat:@"%@",searchData[@"RECEPTION_STATUS"]];
                    model.state = Pmodel.desc;
                    model.ownerName = searchData[@"DELIVERER"];
                    model.ownerMobile = searchData[@"DELIVERER_MOBILE"];
                    model.serverMobile = searchData[@"SERVICE_ADVISOR_MOBILE"];
                    model.serverName = searchData[@"SERVICE_ADVISOR_NAME"];
                    [self.getCarSearchModels addObject:model];
                }
                self.hasNext = YES;
                [self.getCarSearchView.mj_footer endRefreshing];
            } else {
                self.hasNext = NO;
                [self.getCarSearchView.mj_footer endRefreshingWithNoMoreData];
            }
        } else {
            self.hasNext = NO;
            [self.getCarSearchView.mj_footer endRefreshingWithNoMoreData];
        }
        [self.getCarSearchView reloadData];
    } else {
        [self.getCarSearchView.mj_footer endRefreshing];
    }
}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.getCarSearchModels.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ICMGetCarSearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ICMGetCarSearchTableViewCell"];
    [cell setUpICMGetCarSearchTableViewCellWithModel:self.getCarSearchModels[indexPath.row]];
    cell.delegate = self;
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ActualHeight(95);
}

- (void)didClickPreViewWithIndex:(NSIndexPath *)index{
    if ([self.delegate respondsToSelector:@selector(didClickSearchViewWithRECEPTION_NO:andIndex:)]) {
        ICMGetCarSearchModel *model = self.getCarSearchModels[index.row];
        
        [self.delegate didClickSearchViewWithRECEPTION_NO:model.getCarId andIndex:index.row];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.delegate respondsToSelector:@selector(didClickCellWithSearchModel:)]) {
        ICMGetCarSearchModel *model = self.getCarSearchModels[indexPath.row];
        if ([model.RECEPTION_STATUS isEqualToString:@"35521002"]) {
            [self showString:@"接车单已完成！" forSeconds:1.5];
        }else if ([model.RECEPTION_STATUS isEqualToString:@"35521003"]) {
            [self showString:@"接车单已终止！" forSeconds:1.5];
        }
        else {
            [self.delegate didClickCellWithSearchModel:model];
        }
    }
}


@end
